package com.yunkeji.admin.controller.appuser;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.yunkeji.common.annotation.Log;
import com.yunkeji.common.annotation.RepeatSubmit;
import com.yunkeji.common.core.controller.BaseController;
import com.yunkeji.common.core.domain.PageQuery;
import com.yunkeji.common.core.domain.R;
import com.yunkeji.common.core.page.TableDataInfo;
import com.yunkeji.common.core.validate.AddGroup;
import com.yunkeji.common.core.validate.EditGroup;
import com.yunkeji.common.enums.BusinessType;
import com.yunkeji.common.utils.poi.ExcelUtil;
import com.yunkeji.dao.domain.PayWalletLog;
import com.yunkeji.dao.domain.PhoneLog;
import com.yunkeji.dao.vo.AppUserVo;
import com.yunkeji.dao.vo.PayWalletLogVo;
import com.yunkeji.dao.vo.PhoneLogVo;
import com.yunkeji.system.domain.bo.*;
import com.yunkeji.system.service.IAppUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 用户基本信息-扩展(存放一些不重要但是必要的信息)
 *
 * @author zhang
 * @date 2024-07-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/appUser")
public class AppUserController extends BaseController {

    private final IAppUserService iAppUserService;

    /** 设置排序 */
    @SaCheckPermission("system:appUser:toSort")
    @PostMapping("/toSort")
    public R toSort(@RequestBody ToSortBo toSortBo) {
        iAppUserService.toSort(toSortBo);
        return R.ok();
    }
    /** 取消绑定用户 */
    @SaCheckPermission("system:appUser:unBindUser")
    @PostMapping("/unBindUser")
    public R unBindUser(@RequestBody ToBindAccountBo toBindAccountBo) {
        iAppUserService.unBindUser(toBindAccountBo);
        return R.ok();
    }

    /** 绑定用户 */
    @SaCheckPermission("system:appUser:bindUser")
    @PostMapping("/bindUser")
    @Log(title = "绑定下级用户", businessType = BusinessType.INSERT)
    public R bindUser(@RequestBody ToBindAccountBo toBindAccountBo) {
        iAppUserService.bindUser(toBindAccountBo);
        return R.ok();
    }

    /** 设置账号类型 */
    @SaCheckPermission("system:appUser:setAccountType")
    @PostMapping("/setAccountType")
    public R setAccountType(@RequestBody SetAccountTypeBo setAccountTypeBo) {
        iAppUserService.setAccountType(setAccountTypeBo);
        return R.ok();
    }

    /** 踢下线 */
    @SaCheckPermission("system:appUser:kickOnline")
    @GetMapping("/kickOnline/{id}")
    public R kickOnline(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        iAppUserService.kickOnline(id);
        return R.ok();
    }

    /** 封号 */
    @SaCheckPermission("system:appUser:bannedAccount")
    @PostMapping("/bannedAccount")
    public R kickOnline(@RequestBody UserBannedBo userBannedBo) {
        iAppUserService.bannedAccount(userBannedBo);
        return R.ok();
    }

    /** 解封 */
    @SaCheckPermission("system:appUser:releaseAccount")
    @GetMapping("/releaseAccount")
    public R releaseAccount(Long uid) {
        iAppUserService.releaseAccount(uid);
        return R.ok();
    }

    /** 查询用户基本信息-扩展(存放一些不重要但是必要的信息)列表 */
    @SaCheckPermission("system:appUser:list")
    @GetMapping("/list")
    public TableDataInfo<AppUserVo> list(AppUserBo bo, PageQuery pageQuery) {
        return iAppUserService.queryPageList(bo, pageQuery);
    }

    /** 导出用户基本信息-扩展(存放一些不重要但是必要的信息)列表 */
    @SaCheckPermission("system:appUser:export")
    @Log(title = "用户基本信息-扩展(存放一些不重要但是必要的信息)", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppUserBo bo, HttpServletResponse response) {
        List<AppUserVo> list = iAppUserService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户基本信息-扩展(存放一些不重要但是必要的信息)", AppUserVo.class, response);
    }

    /**
     * 获取用户基本信息-扩展(存放一些不重要但是必要的信息)详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:appUser:query")
    @GetMapping("/{id}")
    public R<AppUserVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(iAppUserService.queryById(id));
    }

    /** 新增用户基本信息-扩展(存放一些不重要但是必要的信息) */
    @SaCheckPermission("system:appUser:add")
    @Log(title = "用户基本信息-扩展(存放一些不重要但是必要的信息)", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppUserBo bo) {
        return toAjax(iAppUserService.insertByBo(bo));
    }

    /** 修改用户基本信息-扩展(存放一些不重要但是必要的信息) */
    @SaCheckPermission("system:appUser:edit")
    @Log(title = "用户基本信息-扩展(存放一些不重要但是必要的信息)", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppUserBo bo) {
        return toAjax(iAppUserService.updateByBo(bo));
    }

    /**
     * 删除用户基本信息-扩展(存放一些不重要但是必要的信息)
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:appUser:remove")
    @Log(title = "用户基本信息-扩展(存放一些不重要但是必要的信息)", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iAppUserService.deleteWithValidByIds(Arrays.asList(ids), true));
    }


    /** 添加会员 */
    @SaCheckPermission("system:appUser:addVip")
    @PostMapping("/addVip")
    @Log(title = "手动添加会员", businessType = BusinessType.INSERT)
    public R addVip(@RequestBody AddVipBo addVipBo) {
        iAppUserService.addVip(addVipBo);
        return R.ok();
    }

    /** 查询会员 */
    @SaCheckPermission("system:appUser:vip")
    @GetMapping("/vip/{id}")
    public R addVip(@PathVariable Long id) {
        return R.ok(iAppUserService.getVipInfo(id));
    }

    /** 修改用户视频/语音价格 */
    @SaCheckPermission("system:appUser:changePrice")
    @PutMapping("/changePrice")
    @Log(title = "修改用户视频/语音价格", businessType = BusinessType.UPDATE)
    public R changePrice(@RequestBody AppUserBo bo) {
        iAppUserService.updateByBo(bo);
        return R.ok();
    }

    /** 修改用户手机号码 */
    @SaCheckPermission("system:appUser:changePhone")
    @PutMapping("/changePhone")
    @Log(title = "修改用户手机号码", businessType = BusinessType.UPDATE)
    public R changePhone(@RequestBody PhoneLog log) {
        iAppUserService.changePhone(log);
        return R.ok();
    }

    /** 查看用户手机号码修改记录 */
    @SaCheckPermission("system:appUser:phoneLog")
    @GetMapping("/phoneLog")
    public TableDataInfo<PhoneLogVo> getPhoneLog(PhoneLog log, PageQuery pageQuery) {
        return iAppUserService.getPhoneLog(log, pageQuery);
    }

    /** 修改用户性别 */
    @SaCheckPermission("system:appUser:changeSex")
    @PutMapping("/changeSex")
    @Log(title = "修改用户性别", businessType = BusinessType.UPDATE)
    public R changeSex(@RequestBody AppUserBo bo) {
        iAppUserService.updateByBo(bo);
        return R.ok();
    }

    /** 增加钱包余额 */
    @SaCheckPermission("system:appUser:addWallet")
    @PostMapping("/addWallet")
    @Log(title = "手动添加钱包余额", businessType = BusinessType.INSERT)
    public R addVip(@RequestBody AddWalletBo bo) {
        iAppUserService.addWallet(bo);
        return R.ok();
    }
    /** 查看用户手机号码修改记录 */
    @SaCheckPermission("system:appUser:walletLog")
    @GetMapping("/walletLog")
    public TableDataInfo<PayWalletLogVo> queryPayWalletLog(PayWalletLog log, PageQuery pageQuery) {
        return iAppUserService.queryPayWalletLog(log.getUserId(), pageQuery);
    }
}
